/**
 * 公共操作类
 */
var Common = {
	/**
	 * ajax请求
	 */
	ajax:function(c_type,c_url,c_data,callback){
		$.ajax({
	        type:c_type,
	        url:c_url,
	        data:c_data,
	        success:function(data){
	        	if(data['code']=="0"){
	        		callback(data);
	        	}else{
	        		Common.err(data['notice']);
	        	}
	        }
	    });
	},

	/**
	 * 获取表单json对象
	 */
	getFormJson:function(form_id){
		var param = $("#"+form_id).serialize();
		var param_arr = param.split('&');
		var len = param_arr.length;
		var json = {};
		$.each(param_arr,function(i,val){
			var arr = val.split('=');
			var key = arr[0];
			var value = arr[1];
			json[key] = value;
		});
		return json;
	},

	/**
	 * 判断对象是否为空
	 */
	isEmpty:function(str){
	    if(str=="" || str==null || str=="undefined"){
	        return true;
	    }
	    return false;
	},

	/**
	 * 设置字符串为空
	 */
	setEmpty:function(str){
		if(str=="" || str==null || str=="undefined" || str=="null"){
	        return "";
	    }else{
	    	return str;
	    }
	},

	/**
	 * 判断来源是否是PC端
	 * @returns {Boolean}
	 */
	isPC:function() {
		var sUserAgent = navigator.userAgent.toLowerCase();
		// var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
		var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
		var bIsMidp = sUserAgent.match(/midp/i) == "midp";
		var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
		var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
		var bIsAndroid = sUserAgent.match(/android/i) == "android";
		var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
		var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
		// if (!(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) ){
		//     return true;
		// }else{
		//     return false;
		// }
		if (!(bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid
				|| bIsCE || bIsWM)) {
			return true;
		} else {
			return false;
		}
	},

	/**
	 * 判断是安卓还是IOS
	 */
	isAndroidOrIOS:function(){
		var u = navigator.userAgent, app = navigator.appVersion;
		var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //android终端或者uc浏览器
		var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
		if(isAndroid){
			return 1;
		}else if(isiOS){
			return 2;
		}else{
			return 0;
		}
	},

	/**
	 * 判断来源是否来至微信
	 * @returns {Boolean}
	 */
	isWeixin:function() {
		var ua = navigator.userAgent.toLowerCase();
		if (ua.match(/MicroMessenger/i) == "micromessenger") {
			return true;
		} else {
			return false;
		}
	},

	/**
	 * 获取当前时间
	 */
	getNowFormatDate:function () {
	    var date = new Date();
	    var seperator1 = "-";
	    var seperator2 = ":";
	    var year = date.getFullYear();
	    var month = date.getMonth() + 1;
	    var strDate = date.getDate();
	    if (month >= 1 && month <= 9) {
	        month = "0" + month;
	    }
	    if (strDate >= 0 && strDate <= 9) {
	        strDate = "0" + strDate;
	    }
	    var currentdate = year + seperator1 + month + seperator1 + strDate
	            + " " + date.getHours() + seperator2 + date.getMinutes()
	            + seperator2 + date.getSeconds();
	    return currentdate;
	},
	/**
	 * 获取当前日期【格式为2017-03-15 00:00:00】
	 */
	getNowFormatDay:function () {
	    var date = new Date();
	    var seperator1 = "-";
	    var year = date.getFullYear();
	    var month = date.getMonth() + 1;
	    var strDate = date.getDate();
	    if (month >= 1 && month <= 9) {
	        month = "0" + month;
	    }
	    if (strDate >= 0 && strDate <= 9) {
	        strDate = "0" + strDate;
	    }
	    var currentdate = year + seperator1 + month + seperator1 + strDate
	            + " 00:00:00";
	    return currentdate;
	},

	/**
	 * 电话号码验证
	 */
	isPhoneNum:function(phone){
		var reg = /^1[23456789]\d{9}$/;
        var r = phone.match(reg);
        if(r==null){
        	return false;
        }
		return true;
	},

	/**
	 * 去除空格
	 */
	trim:function(str){
		return str.replace(/(^\s*)|(\s*$)/g, "");
	},

	/**
	 * 比较日期时间大小
	 */
	checkEndTime:function(startTime,endTime){
	    var start = new Date(Date.parse(startTime.replace(/-/g,"/"))).getTime();
	    var end = new Date(Date.parse(endTime.replace(/-/g,"/"))).getTime();
	    if(end<start){
	        return false;
	    }
	    return true;
	},

	/**
	 * 讲中文时间格式转为英文格式
	 * var time 16时20分55秒
	 * return 16:20:55
	 */
	changeTimeFormate:function(time){
		var Ftime=time.replace(/[\u4e00-\u9fa5]+/g,':');
		var Ftime=Ftime.substr(0,Ftime.length-1);
		return Ftime;
	},

	/**
	 * 退出登录
	 */
	logout:function(){
		layer.confirm('确定退出系统吗？', {
    	    btn: ['确定','取消']
    	}, function(){
    		window.location.href = Longmaster.APP+"/index/logout";
    	});
	},

	/**
	 * 账户密码修改
	 */
	change_pwd:function(dgws_uid){
		$("#dgws_uid").val(dgws_uid);
		layer.open({
			type: 1,
			title: false,
			closeBtn: false,
			area: ['600px', "300px"],
			btn: ['确定', '取消'],
			content: $("#modifyPassword"),
			yes: function () {
				if (Common.isEmpty($("#old_password").val())) {
					layer.msg('请填写原密码',{icon:5,shade:[0.5]});
					return false;
				}
				if (Common.isEmpty($("#new_password").val())) {
					layer.msg('请填写新密码',{icon:5,shade:[0.5]});
					return false;
				} else if($("#new_password").val().length < 6) {
					layer.msg('账号密码不能小于6位',{icon:5,shade:[0.5]});
					return false;
				} else if($("#new_password").val().length > 16) {
					layer.msg('账号密码不能大于16位',{icon:5,shade:[0.5]});
					return false;
				}
				if (Common.isEmpty($("#corm_password").val())) {
					layer.msg('请填写确认密码',{icon:5,shade:[0.5]});
					return false;
				}
				if ($("#corm_password").val() != $("#new_password").val()){
					layer.msg('输入两次密码不同',{icon:5,shade:[0.5]});
					return false;
				}
				$("#modifyPasswordForm").ajaxSubmit({
					type: 'post',
					url: Longmaster.APP + "/Index/m_modifypassword",
					success: function (rdata) {
						console.log(rdata)
						if (rdata.code==0){
							layer.msg(rdata.notice,{icon:1,shade:[0.5]},function(){
								window.location.href = Longmaster.APP + "/Index/logout";
							});
						}else{
							layer.msg(rdata.notice,{icon:5,shade:[0.5]});
						}
					}
				});
			}
		});
	},

	/**
	 * 监听高度
	 */
	adaptiveHeight:function(a, baserows, maxrows) {
		var po =  parseInt(a.css('padding-top')) + parseInt(a.css('padding-bottom'));
		var baseLineHeight = parseInt(a.css('line-height'));
		var baseHeight = baseLineHeight * baserows;
		a.height(baseHeight);
		var scrollval = a[0].scrollHeight;
		if (scrollval - po <= baseHeight){
			return false;
		}
		if (scrollval - po >= baseLineHeight * maxrows) {
			scrollval = baseHeight + baseLineHeight * (maxrows-baserows) + po;
		}
		a.height(scrollval - po);
	},

	/**
	 * 监听textarea自适应
	 */
	adaptiveTextarea:function(sel, baserows, maxrows, callback){
		sel.unbind('focus input propertychange');
		sel.bind('focus input propertychange', function(e) {
		    Common.adaptiveHeight($(this), baserows, maxrows);
		    if(callback) callback(e);
		});
		Common.adaptiveHeight(sel, baserows, maxrows);
	},

	/**
	 * 错误提示
	 */
	err:function(tips,callback){
		layer.msg(tips,{icon:5,shade:[0.5]},function(){
			if(callback){
				callback();
			}
		});
	},

	/**
	 * 初始化后台框架
	 */
	initApp:function(){
		var that = this;

	    var $menu = $("#"+Longmaster.menuFlag);
	    $menu.addClass("active");
	    $menu.parent("li").addClass("active");
	    $menu.parent("li").parent("ul").siblings("a").find(".arrow").addClass("open");
	    $menu.parent("li").parent("ul").show();
	    $menu.parent("li").parent("ul").parent("li.sub-menu").addClass("active");

	    //全局ajax请求loading效果
	    $.ajaxSetup({
	        beforeSend:function(){
	            loading = layer.load(1, {
	                shade: [0.3,'#000']
	            });
	        },
	        complete:function(){
	            layer.close(loading);
	        }
	    });
	},
	/**
	 * 重新加载页面
	 */
	reloadPage:function(){
		window.location.reload();
	},

	//是否是正整数
	isPInt:function(str) {
	    var g = /^(0|[1-9]\d*)$/;
	    return g.test(str);
	},
	//是否是整数
	isInt:function(str){
	    var g=/^-?\d+$/;
	    return g.test(str);
	},
	validate:function(num){
		var reg = /^\d+(?=\.{0,1}\d+$|$)/
		return reg.test(num) ;
	},
	/**
	 * 非负小数判断
	 */
	isPlusFloat:function (num){
		var test_reg = /(^\d+$)|(^\d+\.\d+$)/;
		if (Common.isEmpty (num) || !test_reg.test(num)){
			return false;
		}
		return true;
	},
	/**
	 * 判断是否是两个小数，金额判断
	 */
	isMoney:function (num){
		var test_reg = /^([1-9]\d{0,9}|0)([.]?|(\.\d{1,2})?)$/;
		if (Common.isEmpty (num) || !test_reg.test(num)){
			return false;
		}
		return true;
	},

	/**
	 * 判断是安卓还是IOS
	 */
	isAndroidOrIOS:function(){
		var u = navigator.userAgent, app = navigator.appVersion;
		var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //android终端或者uc浏览器
		var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
		if(isAndroid){
			return 1;
		}else if(isiOS){
			return 2;
		}else{
			return 0;
		}
	},

	/**
	 * 处理输入框只能输入数字
	 */
	inputNumInt:function(obj){
		obj.value = obj.value.replace(/[^\d]/g,""); //清除"数字"和"."以外的字符
	},
	/**
	 * 处理输入框只能输入2位小数
	 */
	inputNumFloat:function(obj){
		obj.value = obj.value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
		obj.value = obj.value.replace(/^\./g,""); //验证第一个字符是数字
		obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一个, 清除多余的
		obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
		obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
	},
	/**
	 * 休眠
	 */
	sleep:function(n) {
	    var start = new Date().getTime();
	    while(true){
	    	if(new Date().getTime() - start > n) break;
	    }
    },

	/**
	 * 判断某个值是否在数据中
	 */
    isInArray:function(arr, value){
	    for(var i = 0; i < arr.length; i++){
	        if(value == arr[i]){
	            return true;
	        }
	    }
	    return false;
	},

	/**
	 * 将字符串分割为数组且查询findstr是否存在于arr汇总
	 */
	strToArrExist:function(str, splitstr, findstr)
	{
		if(Common.isEmpty(str) || Common.isEmpty(splitstr) || Common.isEmpty(findstr)){
			return false;
		}
		var arr = str.split(splitstr);
		var eret = arr.indexOf(findstr);
		if(eret == -1){
			return false;
		}else{
			return true;
		}
	},

	/*
	* 查看大图  list 图片列表id
	* */
	showBigImg:function(list){
		layer.ready(function () { //为了layer.ext.js加载完毕再执行
			layer.photos({
				photos:list,
			});
		});
	}


};

/**
 * 检测数组元素索引
 */
Array.prototype.indexOf = function(val) {
	for (var i = 0; i < this.length; i++) {
		if (this[i] == val) return i;
	}
	return -1;
};

/**
 * 检查数组是否包含某个元素
 */
Array.prototype.in_array = function(e){
	for(i=0;i<this.length && this[i]!=e;i++);
	return !(i==this.length);
};

/**
 * 删除数组元素
 */
Array.prototype.remove = function(val) {
	var index = this.indexOf(val);
	if (index > -1) {
		this.splice(index, 1);
	}
};








